package com.zw.yzk.kabu.presenter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.network.entity.BuyExhibitionTicketEntity;
import com.zw.yzk.kabu.data.network.entity.QueryOrderStateEntity;
import com.zw.yzk.kabu.data.network.task.BuyExhibitionTicketFreeTask;
import com.zw.yzk.kabu.data.network.task.BuyExhibitionTicketTask;
import com.zw.yzk.kabu.data.network.task.QueryOrderStateTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.utils.WechatPayInfo;
import com.zw.yzk.kabu.view.activity.MyGoodsOrderActivity;
import com.zw.yzk.kabu.view.activity.MyTicketActivity;
import com.zw.yzk.kabu.view.activity.TicketOrderDetailsActivity;
import com.zw.yzk.kabu.view.view.ConfirmExhibitionTickView;
import com.zw.yzk.kabu.wxapi.WechatManager;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/9.
 */

public class ConfirmExhibitionTickPresenter implements Presenter {

    private UseCase freeBuy;
    private UseCase buy;
    private UseCase checkOrderState;
    private AppCompatActivity activity;
    private ConfirmExhibitionTickView confirmView;
    private String tradeId;
    private String ticketOrderId;


    @Inject
    public ConfirmExhibitionTickPresenter(@Named("buyFreeUseCase") UseCase freeBuy,
                                          @Named("buyUseCase") UseCase buy,
                                          @Named("queryOrderUseCase") UseCase checkOrderState,
                                          AppCompatActivity activity) {
        this.freeBuy = freeBuy;
        this.buy = buy;
        this.checkOrderState = checkOrderState;
        this.activity = activity;
    }

    public void setView(ConfirmExhibitionTickView view) {
        this.confirmView = view;
    }

    public void buyTicketFree(int exhibitionId, int ticketId, int count, String phone) {
        freeBuy.execute(new FreeBuySubscriber(activity), new BuyExhibitionTicketFreeTask(exhibitionId, ticketId, count, phone));
    }

    public void buyTicket(int exhibitionId, String phone, int ticketId, int count) {
        buy.execute(new BuyTicketSubscriber(activity), new BuyExhibitionTicketTask(exhibitionId, phone, ticketId, count));
    }

    public void checkOrderState() {
        if (TextUtils.isEmpty(tradeId)) {
            return;
        }
        checkOrderState.execute(new QueryOrderStateSubscriber(activity), new QueryOrderStateTask(tradeId));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        freeBuy.unSubscribe();
        buy.unSubscribe();
        checkOrderState.unSubscribe();
    }

    private void startTickerInfoActivity(String ticketOrderId) {
        if (TextUtils.isEmpty(ticketOrderId)) {
            activity.startActivity(new Intent(activity, MyTicketActivity.class));
        } else {
            Intent intent = new Intent(activity, TicketOrderDetailsActivity.class);
            intent.putExtra(ActivityConstant.EXHIBITION_TICKET_ORDER_ID, ticketOrderId);
            activity.startActivity(intent);
        }
        activity.finish();
    }

    private class QueryOrderStateSubscriber extends BaseSubscriber<QueryOrderStateEntity> {

        public QueryOrderStateSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onNext(QueryOrderStateEntity entity) {
            super.onNext(entity);
            if (entity.result.result == 1) {
                ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.paid_success));
//                confirmView.queryOrderStateSuccess(ticketOrderId);
            }
            startTickerInfoActivity(ticketOrderId);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            startTickerInfoActivity(null);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.check_order_state_by_yourself);
        }
    }

    private class FreeBuySubscriber extends BaseSubscriber<Boolean> {

        public FreeBuySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Boolean aBoolean) {
            super.onNext(aBoolean);
            ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.buy_exhibition_ticket_success));
            confirmView.buyTicketSuccess();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.buy_exhibition_ticket_failed);
        }
    }

    private class BuyTicketSubscriber extends BaseSubscriber<BuyExhibitionTicketEntity> {

        public BuyTicketSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(BuyExhibitionTicketEntity entity) {
            super.onNext(entity);

            //设置支付信息
            tradeId = entity.result.tradeId;
            ticketOrderId = entity.result.orderId;
            //调起微信支付
            WechatManager.payByWeChat(activity, entity.result.weChatPay.appId, entity.result.weChatPay.partnerId, entity.result.weChatPay.prepayId,
                    entity.result.weChatPay.packageName, entity.result.weChatPay.nonceStr, entity.result.weChatPay.timeStamp, entity.result.weChatPay.sign);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.buy_ticket_failed);
        }
    }
}
